{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>大纲<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#创建一颗树\" data-toc-modified-id=\"创建一颗树-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>创建一颗树</a></span></li><li><span><a href=\"#树操作\" data-toc-modified-id=\"树操作-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>树操作</a></span></li><li><span><a href=\"#节点操作\" data-toc-modified-id=\"节点操作-3\"><span class=\"toc-item-num\">3&nbsp;&nbsp;</span>节点操作</a></span></li></ul></div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "工作需要使用树,自己实现成本大,找到一个开源的项目,正好可以满足需求,感谢作者."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 创建一颗树\n",
    "1. 可以创建任意形式的树,比如:二叉树,三叉树\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-16T06:19:57.229918Z",
     "start_time": "2018-05-16T06:19:57.223923Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '王凯旋'}\n",
      "├── question2\n",
      "├── question2\n",
      "└── question2\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from treelib import Node\n",
    "from treelib import Tree\n",
    "tree = Tree()\n",
    "tree.create_node({'name':'王凯旋'},'id1')\n",
    "tree.create_node('question2','id2','id1')\n",
    "tree.create_node('question2','id3','id1')\n",
    "tree.create_node('question2','id4','id1')\n",
    "tree.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 树操作\n",
    "- 如果tag的数据类型只支持str,则无法展开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-16T06:26:12.179660Z",
     "start_time": "2018-05-16T06:26:12.173894Z"
    }
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "sequence item 0: expected str instance, dict found",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-5-22154bfbede6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m''\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mtree\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnode\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtag\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mnode\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mtree\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexpand_tree\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mTree\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDEPTH\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: sequence item 0: expected str instance, dict found"
     ]
    }
   ],
   "source": [
    "print(''.join([tree[node].tag for node in tree.expand_tree(mode = Tree.DEPTH)]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 节点数据类型一致,就能展开了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-16T06:41:17.820649Z",
     "start_time": "2018-05-16T06:41:17.816976Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "王凯旋question1question2question3\n"
     ]
    }
   ],
   "source": [
    "tree1 = Tree()\n",
    "tree1.create_node('王凯旋','id1')\n",
    "tree1.create_node('question1','id2','id1')\n",
    "tree1.create_node('question2','id3','id1')\n",
    "tree1.create_node('question3','id4','id1')\n",
    "print(''.join([tree1[node].tag for node in tree1.expand_tree(mode = Tree.DEPTH)]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**删除节点 **"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-16T06:41:19.180677Z",
     "start_time": "2018-05-16T06:41:19.178175Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "王凯旋\n",
      "├── question1\n",
      "└── question3\n",
      "\n"
     ]
    }
   ],
   "source": [
    "tree1.remove_node('id3')\n",
    "tree1.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-16T06:42:40.225862Z",
     "start_time": "2018-05-16T06:42:40.219133Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "王凯旋\n",
      "├── question2\n",
      "└── question3\n",
      "    └── question1\n",
      "\n"
     ]
    }
   ],
   "source": [
    "tree1 = Tree()\n",
    "tree1.create_node('王凯旋','id1')\n",
    "tree1.create_node('question1','id2','id1')\n",
    "tree1.create_node('question2','id3','id1')\n",
    "tree1.create_node('question3','id4','id1')\n",
    "tree1.move_node('id2','id4')\n",
    "tree1.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**获取树高度**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-16T06:43:11.890453Z",
     "start_time": "2018-05-16T06:43:11.885239Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tree1.depth()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 节点操作\n",
    "**获取节点的高度**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-05-16T06:44:17.032082Z",
     "start_time": "2018-05-16T06:44:17.028266Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "node = tree1.get_node('id3')\n",
    "tree1.depth(node)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda all lib",
   "language": "python",
   "name": "base"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "大纲",
   "title_sidebar": "目录",
   "toc_cell": true,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
